﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading.Tasks;
using System.Threading;
using Dapper;

namespace SHRMS.childForm
{
    public partial class childForm_personalInfo : Form
    {
        private IDbConnection dbConnection;
        private readonly string _userid;
        public childForm_personalInfo(string UserID)
        {
            InitializeComponent();
            dbConnection = new DbFactory().DefaultDbConnection;
            _userid = UserID;
            panel_changePwd02.Visible = false;
        }

        private void btn_showCPwd_Click(object sender, EventArgs e)
        {
            panel_changePwd02.Visible = true;
            panel_changePwd01.Visible = false;
        }

        private void btn_cPwdDone_Click(object sender, EventArgs e)
        {
            string oldPwd = textBox_oldPwd.Text;
            string newPwd01 = textBox_newPwd01.Text;
            string newPwd02 = textBox_newPwd02.Text;
            if (newPwd01 != newPwd02)
            {
                MessageBox.Show("您输入的新密码不一致！请确认后再输入！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                string sqlStr_getoldPwd = $"select password from t_user where userid = '{_userid}';";
                if (oldPwd != dbConnection.ExecuteScalar<string>(sqlStr_getoldPwd))
                {
                    MessageBox.Show("你输入的旧密码不正确，请确认后重试！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    string sqlstr_update = $"update t_user set password='{newPwd01} ' where userid = '{_userid}';";
                    var result = dbConnection.Execute(sqlstr_update);
                    if (result > 0)
                    {
                        MessageBox.Show("修改密码成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("修改密码失败！","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void button_cPwdCancel_Click(object sender, EventArgs e)
        {
            panel_changePwd02.Visible = false;
            panel_changePwd01.Visible = true;
        }

        private void childForm_personalInfo_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            string sql_getname = $"select username from t_user where userid='{_userid}'";
            string UserName = dbConnection.QuerySingleOrDefault<string>(sql_getname);
            label_hello.Text = "Hello  " + UserName + "!";
            label_uname.Text = UserName;

            string sqlStr_getRole = $"select empNo,role,email from t_user where userid = '{_userid}';";
            var reader = dbConnection.ExecuteReader(sqlStr_getRole);
            reader.Read();
            string empNo = reader["empNo"].ToString();
            string email = reader["email"].ToString();
            string role = reader["role"].ToString();
            reader.Close();
            label_empNo.Text = empNo;
            label_email.Text = email;
            label_role.Text = role;

            //封装委托，并创建线程任务、读取t_attendance表
            Task.Run(async() =>
            {
                string sqlStr_att = $"select * from t_attendance where empNo = '{empNo}';";
                var reader_att =await dbConnection.ExecuteReaderAsync(sqlStr_att);
                reader_att.Read();

                label_late.Text = reader_att["lateNumber"].ToString() + " 次";
                label_earlyLeave.Text = reader_att["earlyLeaveNumber"].ToString() + " 次";
                label_kuang.Text = reader_att["absenteeism"].ToString() + " 次";
                label_shouldDay.Text = reader_att["attendanceDays"].ToString() + " 天";
                label_realDay.Text = reader_att["actualDays"].ToString() + " 天";
                label_overTime.Text = reader_att["overtimeHours"].ToString() + " 小时";
                reader_att.Close();
            });

            //封装委托，并创建线程任务、读取t_salary表
            Task.Run(async() =>
            {
                string sqlStr_sal = "select * from t_salary where empNo = '" + empNo + "';";
                var reader_sal =await dbConnection.ExecuteReaderAsync(sqlStr_sal);
                reader_sal.Read();

                label_baseSalary.Text = reader_sal["baseSalary"].ToString() + " 元";
                label_jiangjin.Text = reader_sal["bonus"].ToString() + " 元";
                label_butie.Text = reader_sal["subsidies"].ToString() + " 元";
                label_jiabanfei.Text = reader_sal["overtimePay"].ToString() + " 元";
                label_yingkou.Text = reader_sal["deductPay"].ToString() + " 元";
                label_yingfa.Text = reader_sal["salary"].ToString() + " 元";
                label_shuilv.Text = (Convert.ToDouble(reader_sal["rate"]) * 100).ToString() + " %";
                label_realSalary.Text = reader_sal["realSalary"].ToString() + " 元";
                reader_sal.Close();
            });

            //封装委托，并创建线程任务、进行联表查询获取基本信息
            Task.Run(async() =>
            {
                string sqlStr_Info = $@"select 
	tea.name,dept.deptName,tea.gender,tea.age
from 
	t_teacher tea
join
	t_department dept
on
	tea.departmentNo = dept.departmentNo
where
	tea.teacherNo = '{empNo}'";
                var reader_Info =await dbConnection.ExecuteReaderAsync(sqlStr_Info);
                reader_Info.Read();

                label_name.Text = reader_Info[0].ToString();
                label_dept.Text = reader_Info[1].ToString();
                label_gender.Text = reader_Info[2].ToString();
                label_age.Text = reader_Info[3].ToString();
                reader_Info.Close();
            });
        }
    }
}
